package models;

import models.admin.AdminBusiness;
import models.constants.AvailableStatus;
import models.constants.DeletedStatus;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.util.List;
import java.util.Map;

/**
 * Created by liming on 16/7/12.
 */
@Entity
@Table(name = "admin_supplier")
public class AdminSupplier extends BaseModel {
    /**
     * 供应商代码
     */
    @Column(name = "code")
    public String code;

    /**
     * 供应商名称
     */
    @Column(name = "name")
    public String name;

    /**
     * 拼音简写
     */
    @Column(name = "pingyin")
    public String pinyin;

    /**
     * 联系电话
     */
    @Column(name = "phone")
    public String phone;

    /**
     * 税号
     */
    @Column(name = "tax_account")
    public String taxAccount;

    /**
     * 银行帐号
     */
    @Column(name = "bank_account")
    public String bankAccount;

    /**
     * 地址
     */
    @Column(name = "address")
    public String address;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;

    /**
     * 使用状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "available_status")
    public AvailableStatus availableStatus;

    /**
     * 根据ID删除供应商
     * @param id
     */
    public static void deleteById(long id){
        AdminSupplier.delete("id = ?",id);
    }

    /**
     * 删除状态
     */
    @Column(name = "deleted_status")
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted = DeletedStatus.UN_DELETED;


    /**
     * 获取供应商列表
     */

    public static List<AdminSupplier> loadAllSupplier(){
        return AdminSupplier.find("availableStatus=? and deleted = ? ",AvailableStatus.AVAILABLE , DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 分页查询.
     */
    public static JPAExtPaginator<AdminSupplier> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        // 拼接sql
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED")
                .append("/~ and t.id = {id} ~/")
                .append("/~ and t.code like {code} ~/") //编号
                .append("/~ and t.name like {name} ~/") //名称
                .append("/~ and t.phone like {phone} ~/"); //创建时间

        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<AdminSupplier> orderPage = new JPAExtPaginator<>("AdminSupplier t", "t", AdminSupplier.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }


    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public static AdminSupplier findBySupplierId(long id){
        return AdminSupplier.find("id = ? and deleted = ? " , id  , DeletedStatus.UN_DELETED).first();
    }

}
